package de.fuberlin.processing.who.cancer.data;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import de.fuberlin.processing.who.cancer.utils.Notification;


public class DatabaseConfig {
	
	public static boolean overidePreSettings = true;
	
	public static String dbDatabase = "";
	public static String dbProtocol = "";
	public static String dbUser = "";
	public static String dbPassword = "";
	public static String dbHost = "";
	public static String dbPort = "";
	public static String dbDriver = "";
	
	public static void loadConfig() throws DBException {

		try {
			Properties properties = new Properties();

			try {
				properties.load( new FileInputStream( "./config/dbconfig"));
			} catch ( Exception e) {
				try {
					properties.load( new FileInputStream( "../config/dbconfig"));
				} catch ( Exception e2) {
					InputStream in = DatabaseConfig.class.getResourceAsStream( "dbconfig");
					properties.load( in);
				}
			}

			if ( dbDatabase.isEmpty() || overidePreSettings)
			  dbDatabase = properties.getProperty( "database");
			if ( dbProtocol.isEmpty() || overidePreSettings)
				dbProtocol = properties.getProperty( "protocol");
			if ( dbUser.isEmpty() || overidePreSettings)
				dbUser = properties.getProperty( "user");
			if ( dbPassword.isEmpty() || overidePreSettings)
				dbPassword = properties.getProperty( "password");
			if ( dbHost.isEmpty() || overidePreSettings)
				dbHost = properties.getProperty( "host");
			if ( dbPort.isEmpty() || overidePreSettings)
				dbPort = properties.getProperty( "port");
			if ( dbDriver.isEmpty() || overidePreSettings)
				dbDriver = properties.getProperty( "driver");

		} catch ( Exception e) {
			Notification.printDebugException( e);
			throw new DBException( "Cannot read database configuration file.");
		}
	}
	
	
	
	
	

}
